package com.carryGo.service.dp;


import com.carryGo.dao.dp.UserDao;
import com.carryGo.entity.User;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

/**
 * ClassName:UserService
 * package:guoban
 * Desription:
 * 实现数据的增删改查
 * @Date:2020/8/23 17:17
 * @Author: 邓攀
 */
public class UserService extends UserDao {

    /**
     * 查询
     */
    public Object query(Object obj) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        //1.获取对象的反射
        Class clas = obj.getClass();
        //获取类名
        String tableName = clas.getSimpleName();
        //2.拼接sql
        String sql = "select * from " + tableName + " where id = ?";
        return findBean(clas,sql,clas.getDeclaredMethod("getId").invoke(obj));
    }

    /**
     * 删除
     */
    public int delete(Object obj) throws Exception {
        //1.获取对象的反射
        Class clas = obj.getClass();
        //获取类名
        String tableName = clas.getSimpleName();
        //2.拼接sql
        String sql = "delete from "+tableName +" where id = ?";

        return update(sql, clas.getDeclaredMethod("getId").invoke(obj));
    }

    /**
     * 所有的表的插入都可以用这个方法
     */
    public int insert(Object obj) throws Exception {
        //1.获取对象的反射
        Class clas = obj.getClass();
        //获取类名
        String tableName = clas.getSimpleName();
        //2.拼接sql
        String sql = "INSERT INTO "+tableName;
        //参数部分
        String sqlParams = "(";
        //占位符部分
        String sqlZ = "(";
        //循环所有的字段
        Field[] fields = clas.getDeclaredFields();
        //参数
        Object[] params = new Object[fields.length-1];
        int i = 0;
        for (Field field : fields) {
            String name = field.getName();
            if (name.equals("id"))
                continue;
            sqlParams += name+",";
            sqlZ += "?,";
            //调用get方法
            //获取方法
            String getMethodName = "get" + name.substring(0,1).toUpperCase() + name.substring(1);
            params[i] = clas.getDeclaredMethod(getMethodName).invoke(obj);
            i++;
        }
        //去掉最后一个逗号
        sqlParams = sqlParams.substring(0, sqlParams.length()-1);

        sqlZ = sqlZ.substring(0, sqlZ.length()-1);
        sqlParams += ")";
        sqlZ += ")";

        sql = sql + sqlParams + " values " + sqlZ;

        return update(sql, params);
    }

    /**
     * 所有的表的更新都可以用这个方法
     */
    public int update(Object obj) throws Exception {
        //1.获取对象的反射
        Class clas = obj.getClass();
        //获取类名
        String tableName = clas.getSimpleName();
        //2.拼接sql
        String sql = "UPDATE " + tableName +" SET ";
        //参数部分
        String sqlParams = "";
        //循环所有的字段
        Field[] fields = clas.getDeclaredFields();
        //参数
        Object[] params = new Object[fields.length];
        int i = 0;
        for (Field field : fields) {
            String name = field.getName();
            if (name.equals("id"))
                continue;
            sqlParams += name+"=?,";
            //调用get方法
            //获取方法
            String getMethodName = "get" + name.substring(0,1).toUpperCase() + name.substring(1);
            params[i] = clas.getDeclaredMethod(getMethodName).invoke(obj);
            i++;
        }
        //去掉最后一个逗号
        sqlParams = sqlParams.substring(0, sqlParams.length()-1);

        sql = sql + sqlParams + " where id = ?";
        //最后一个位置放入id
        params[params.length-1] = clas.getDeclaredMethod("getId").invoke(obj);

        return update(sql, params);
    }


    /**
     * 增加用户数据
     * @param user
     * @return
     */
    public boolean addUser(User user) {
        try {
            return insert(user) > 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 通过用户名查询用户数据
     * @param username 要查询的用户名
     * @return
     */
    public User queryUserByUsername(String username) {
        String sql = "select * from user where username = ?";
        return findBean(User.class, sql, username);
    }

    /**
     * 通过密码查询用户数据
     */
    public User queryUserByPassword(String password) {
        String sql = "select * from user where password = ?";
        return findBean(User.class, sql, password);
    }

    /**
     * 更改密码
     */
    public boolean updateUser(User user) {
        try {
            boolean result =  update(user)>0;
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 查询单条数据局
     */
    public User queryUser(int id) {

        try {
            return (User) query(new User(id));
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }


}
